package com.ruge.gateway.checker;

import com.ruge.core.exception.BaseBusinessException;
import com.ruge.core.tool.RequestTool;
import com.ruge.gateway.error.ApiGatewayError;
import com.ruge.tool.str.StringTool;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;

import java.util.List;
import java.util.Map;

/**
 * @program: ruge-cloud
 * @description:
 * @author: ruge.wu
 * @create: 2022-01-08 19:57
 **/
@Component
public class UserTokenChecker {
    private static final String TOKEN = "token";
//    @Resource
//    private UserFeign userFeign;


    public void check(String token) {
        try {
            // 调用tokenCheck服务检查token
//            userFeign.checkToken(token);
        } catch (Exception e) {
            throw new BaseBusinessException(ApiGatewayError.UNKNOWN_TOKEN_ERROR);
        }
    }

    public String getRequestToken(ServerWebExchange exchange) {
        Map<String, List<String>> headerMap = RequestTool.getHeaderMap(exchange);
        if (StringTool.isBlank(headerMap.get(TOKEN))) {
            throw new BaseBusinessException(ApiGatewayError.TOKEN_NOT_FOND);
        }
        return headerMap.get(TOKEN).get(0);
    }
}
